//
//  ViewController.m
//  UI20_NSURLSession
//
//  Created by dllo on 16/4/6.
//  Copyright © 2016年 XWH. All rights reserved.
//

#import "ViewController.h"
#import "NetworkHandler.h"
#import "ModelOfdata.h"
@interface ViewController ()<NetworkHandleDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataSouce;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self handleData];
    [self creatTableView];
}



-(void)handleData{
    
  [NetworkHandler neworkHandlerJSONWithUrl:@"http://api.m.mtime.cn/News/NewsList.api?pageIndex=1" delegate:self];
    
    
}

-(void)didFinishHandler:(id)result{
    
    self.dataSouce = [NSMutableArray array];
    
    for (NSDictionary *dic in result[@"newsList"]) {
        ModelOfdata *model = [[ModelOfdata alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        
        [self.dataSouce addObject:model];
    }
    
    [self.tableView reloadData];
}

-(void)creatTableView{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    [self.view addSubview:self.tableView];
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    ModelOfdata *model = self.dataSouce[indexPath.row];
    
    cell.textLabel.text = model.title;
    
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
